// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129


module.exports = {
	_before: function () { // 通用预处理器

	},
	/**
	 * 获取记录列表
	 */
	async getList(pageIndex, pageSize, year, month, openid) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		let yearReg = year, monthReg = month > 9 ? month : ('0'+month), filterReg;
		if (year === '全部') {
			yearReg = '\\d+'
		}
		if (month === '全部') {
			monthReg = '\\d+'
		}
		const res = await dbjql.collection('travels').where({
			date: new RegExp(`${yearReg}-${monthReg}-\\d+`),
			onlyid: openid
		}).skip((pageIndex - 1) * pageSize).limit(pageSize).get();
		return res;
	},
	/**
	 * 添加记录
	 * @param {Object} item
	 */
	async add(item) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('travels').add(item);
		return res;
	},
	/**
	 * 查询记录通过id
	 * @param {Object} id
	 */
	async query(id) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('travels').doc(id).get();
		return res;
	},
	/**
	 * 更新记录
	 */
	async update(id, item) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('travels').doc(id).update(item);
		return res;
	},
	/**
	 * 更新记录
	 */
	async delete(id) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('travels').doc(id).remove();
		return res;
	}
}
